using SS;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TVSGAS.core
{
	internal class Directory
	{
		private Func objFunc = new Func();
		private string serverName = "";
		private string Uid = "";
		private string Pwd = "";
		private string StrConnectionString = "";
		private string currStringConnection = "";
		public Directory()
		{
			this.serverName = ConfigurationManager.AppSettings["ServerName"].ToString();
			this.Uid = ConfigurationManager.AppSettings["UserID"].ToString();
			this.Pwd = ConfigurationManager.AppSettings["PassWord"].ToString();
			this.currStringConnection = Program.strConnectionString;
		}
		public void getInfo(string ID, Label lblName, Label lblAddress)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT Onwer,Address FROM [TabPhoneData] WHERE [Number] LIKE N'%" + ID + "%'";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					lblName.Text = sqlDataReader["Onwer"].ToString();
					lblAddress.Text = sqlDataReader["Address"].ToString();
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
		private void Action(string Phone, string Onwer, string Address)
		{
			SqlConnection sqlConnection = new SqlConnection(this.currStringConnection);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "ActionOnTabPhoneData";
			sqlCommand.CommandType = CommandType.StoredProcedure;
			sqlCommand.Parameters.Add("Number", SqlDbType.VarChar, 20);
			sqlCommand.Parameters.Add("Onwer", SqlDbType.NVarChar, 500);
			sqlCommand.Parameters.Add("Address", SqlDbType.NVarChar, 500);
			sqlCommand.Parameters["Number"].Value = Phone;
			sqlCommand.Parameters["Onwer"].Value = Onwer;
			sqlCommand.Parameters["Address"].Value = Address;
			sqlCommand.ExecuteNonQuery();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int getCountPhone()
		{
			int result = 0;
			SqlConnection sqlConnection = new SqlConnection(this.currStringConnection);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT Count(*) AS CountItem FROM [TabPhoneData]";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = int.Parse(sqlDataReader["CountItem"].ToString());
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public void loadDataNew(string DataBase)
		{
			this.StrConnectionString = string.Concat(new string[]
			{
				"Server=",
				this.serverName,
				"; Initial Catalog=",
				DataBase,
				"; User Id=",
				this.Uid,
				"; pwd=",
				this.Pwd,
				";"
			});
			SqlConnection sqlConnection = new SqlConnection(this.StrConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT * FROM [TabPhoneData]";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				this.Action(sqlDataReader["Number"].ToString(), sqlDataReader["Onwer"].ToString(), sqlDataReader["Address"].ToString());
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
	}
}
